package com.resjz.milddog.service.dept;

import cn.hutool.core.bean.BeanUtil;
import com.purgeteam.cloud.dispose.starter.Result;
import com.purgeteam.cloud.dispose.starter.exception.error.CommonErrorCode;
import com.resjz.milddog.Interface.dept.DeptInterface;
import com.resjz.milddog.Interface.user.UserInterface;
import com.resjz.milddog.vo.Role.UserRoleVo;
import com.resjz.milddog.vo.dept.DeptVo;
import com.resjz.milddog.vo.menu.MenuVo;
import com.resjz.milddog.vo.user.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DeptService {
    //引入dubbo
    @DubboReference(version = "${dubbo.service.version}",check = false)
    private DeptInterface deptInterface;
    @DubboReference(version = "${dubbo.service.version}",check = false)
    private UserInterface userInterface;

    public Result addSave(DeptVo deptVo){
        if(deptInterface.saveDept(deptVo)) return Result.ofSuccess("保存成功");
        else return Result.ofFail(CommonErrorCode.BUSINESS_ERROR);
    }
    public List<DeptVo> getDeptArr(){
        return deptInterface.getDeptArr();
    }
    public Result delDept(Long deptId){
        if(! deptInterface.isHasDeptById(deptId)) return Result.ofFail(CommonErrorCode.PARAM_ERROR);
        if(deptInterface.hasSubDept(deptId)) return Result.ofFail("1","有子部门不能删除");
        if(userInterface.userInDeptCnt(deptId).compareTo(0)>0) return Result.ofFail("1","该部门下有成员，不能删除");
        deptInterface.delDept(deptId,false);
        return Result.ofSuccess("删除成功");
    }
}
